﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.Common.Utils.InterestAmt
{
   public class InterestHelper
    {
        /// <summary>
        /// 方式：等本金还款
        /// </summary>
        /// <param name="nDeadline">期限</param>
        /// <param name="fMoney">总金额</param>
        /// <param name="dRate">年利率</param>
        /// <returns>交错数组:(1.月还款额,2.月利息)</returns>
        public static double[][] F_MonthMoney(int nDeadline, double fMoney, double yRate)
        {
            double[][] arr = new double[2][];//声明交错数组

            double[] fPrincipal = new double[nDeadline]; //本金
            double[] fSparePrin = new double[nDeadline];//剩余本金
            double[] fMonthRate = new double[nDeadline];//月利息
            double[] fMonthMoney = new double[nDeadline];//月还款额

            for (int i = 0; i < nDeadline; i++)
            {
                fPrincipal[i] = fMoney / nDeadline;
                if (i == 0)
                {
                    fSparePrin[i] = fMoney - fPrincipal[i];//剩余本金
                    fMonthRate[i] = Math.Round(fMoney * (yRate / 12), 2);//月利息
                }
                else
                {
                    fSparePrin[i] = fSparePrin[i - 1] - fPrincipal[i];//剩余本金
                    fMonthRate[i] = Math.Round(fSparePrin[i - 1] * (yRate / 12), 2);//月利息=上个月的剩余本金×利率
                }

                fMonthMoney[i] = Math.Round(fPrincipal[i] + fMonthRate[i], 2);
            }

            arr[0] = fMonthMoney;//月还款
            arr[1] = fMonthRate;//月利息

            return arr;
        }
        /// <summary>
        /// 方式：等本息还款
        /// </summary>
        /// <param name="nDeadline">期限</param>
        /// <param name="fMoney">总金额</param>
        /// <param name="dRate">年利率</param>
        /// <returns>交错数组:(1.月还款额,2.本金，3.利息)</returns>
        public static double[][] D_TotalMoney(int nDeadline, double fMoney, double yRate)
        {
            double[][] arr = new double[3][];//声明交错数组

            int i;
            double tlnAcct = 0, tdepAcct = 0;

            double[] lnAcctbal = new double[nDeadline]; /*贷款余额*/
            double[] depAcctbal = new double[nDeadline]; /*总还款*/
            double[] payrateAcct = new double[nDeadline]; /*每月应还利息*/
            double[] payAcct = new double[nDeadline]; /*每月应还款*/
            double[] paybaseAcct = new double[nDeadline]; /*每月应还本金*/

            tlnAcct = fMoney;

            for (i = 0; i < nDeadline; i++)
            {
                paybaseAcct[i] = Math.Round((Math.Pow((1 + yRate / 12), i + 1) - Math.Pow((1 + yRate / 12), i)) / (Math.Pow((1 + yRate / 12), nDeadline) - 1) * fMoney, 2);
                payrateAcct[i] = Math.Round(tlnAcct * yRate / 12, 2);
                payAcct[i] = Math.Round(paybaseAcct[i] + payrateAcct[i], 2);
                lnAcctbal[i] = tlnAcct - paybaseAcct[i];
                depAcctbal[i] = tdepAcct + payAcct[i];
                tdepAcct = depAcctbal[i];
                tlnAcct = tlnAcct - paybaseAcct[i];
            }

            arr[0] = payAcct;//月还款
            arr[1] = paybaseAcct;//月本金
            arr[2] = payrateAcct;//月利息

            return arr;
        }

       /// <summary>
       /// 投资送积分
       /// </summary>
       /// <param name="tenderAmt">投资金额</param>
       /// <param name="day">天数</param>
       /// <returns></returns>
        public static int GetTenderIntegral(decimal tenderAmt,int day)
        {
            if (tenderAmt > 0 && day > 0)
            {
                decimal integral = Math.Floor((tenderAmt * day)/1000);
                return int.Parse(integral.ToString());
            }
            else
            {
                return 0;
            }
        }

    }
}
